package com.dongnam.quanly.common.model;

import java.io.Serializable;
import java.sql.Timestamp;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.Version;

@Entity
@Table(name = "consoleuser")
// @org.hibernate.annotations.BatchSize(size = 10)
public class ConsoleUser implements Serializable {

	private static final long serialVersionUID = 1L;

	// private static final Logger logger =
	// LoggerFactory.getLogger(ConsoleUser.class);

	public static enum Status {
		ACTIVE("A"), INACTIVE("I"), ;
		public final String id;

		private Status(String id) {
			this.id = id;
		}

		public static Status getEnum(String id) {
			for (Status obj : Status.values()) {
				if (obj.id.equals(id)) {
					return obj;
				}
			}
			return null;
		}

		@Override
		public String toString() {
			return id; // to be used by hibernate, just #name() to get the enum
						// name
		}
	}

	@Id
	@Column(name = "USERID", length = 30)
	private String userId;
	@Column(name = "NAME", length = 60)
	private String name;
	@Column(name = "PASSWORD", length = 50, nullable = false)
	private String password;
	@Column(name = "STATUS", length = 10, nullable = false)
	@Enumerated(EnumType.STRING)
	private Status status;
	@Column(name = "LOGINFAILURE")
	private Integer loginFailure;
	@Column(name = "LASTLOGIN")
	private Timestamp lastLogin;
	@Column(name = "CREATEDATE")
	private Timestamp createDate;
	// @Temporal(TemporalType.TIMESTAMP)
	@Version
	@Column(name = "LASTUPDATEDON")
	private Timestamp lastUpdatedOn;
	@Column(name = "LASTUPDATEDBY", length = 30)
	private String lastUpdatedBy;
	@Transient
	private String groupId; // for use by console only

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "GROUPID", nullable = false)
	@org.hibernate.annotations.ForeignKey(name = "FK_CONSOLEUSER_USERGROUP")
	private UserGroup group;

	public ConsoleUser() {
		loginFailure = 0;
		createDate = new Timestamp(System.currentTimeMillis());
	}

	public ConsoleUser(String userId, String name, String password, Status status, Integer loginFailure,
			Timestamp lastLogin, String lastUpdatedBy) {
		super();
		this.userId = userId;
		this.name = name;
		this.password = password;
		this.status = status;
		this.loginFailure = loginFailure;
		this.lastLogin = lastLogin;
		this.lastUpdatedBy = lastUpdatedBy;
		createDate = new Timestamp(System.currentTimeMillis());
	}

	public ConsoleUser(String userId, String name, String password, Timestamp createDate, String status) {
		this.userId = userId;
		this.name = name;
		this.password = password;
		this.createDate = createDate;
		if (status.equals("ACTIVE"))
			this.status = Status.ACTIVE;
		else if (status.equals("INACTIVE"))
			this.status = Status.INACTIVE;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Status getStatus() {
		return status;
	}

	public void setStatus(Status status) {
		this.status = status;
	}

	public Integer getLoginFailure() {
		return loginFailure;
	}

	public void setLoginFailure(Integer loginFailure) {
		this.loginFailure = loginFailure;
	}

	public Timestamp getLastLogin() {
		return lastLogin;
	}

	public void setLastLogin(Timestamp lastLogin) {
		this.lastLogin = lastLogin;
	}

	public Timestamp getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Timestamp createDate) {
		this.createDate = createDate;
	}

	public Timestamp getLastUpdatedOn() {
		return lastUpdatedOn;
	}

	public void setLastUpdatedOn(Timestamp lastUpdatedOn) {
		this.lastUpdatedOn = lastUpdatedOn;
	}

	public String getLastUpdatedBy() {
		return lastUpdatedBy;
	}

	public void setLastUpdatedBy(String lastUpdatedBy) {
		this.lastUpdatedBy = lastUpdatedBy;
	}

	/**
	 * Warning: to be used by console only
	 */
	public String getGroupId() {
		return groupId;
	}

	/**
	 * Warning: to be used by console only
	 */
	public void setGroupId(String groupId) {
		this.groupId = groupId;
	}

	public UserGroup getGroup() {
		return group;
	}

	/**
	 * If maintaining bidirectional relationship of Java object is needed, use
	 * {@link UserGroup#addConsoleUser(ConsoleUser)}. If for performance, use
	 * this method.
	 */
	public void setGroup(UserGroup group) {
		this.group = group;
	}

	public String toString() {
		StringBuffer buffer = new StringBuffer();
		buffer.append("userId:" + userId + "; ");
		buffer.append("name:" + name + "; ");
		buffer.append("password:" + password + "; ");
		buffer.append("status:" + status + "; ");
		buffer.append("loginFailure:" + loginFailure + "; ");
		if (lastLogin != null) {
			buffer.append("lastLogin:" + lastLogin + "; ");
		}
		if (createDate != null) {
			buffer.append("createDate:" + createDate + "; ");
		}
		if (lastUpdatedOn != null) {
			buffer.append("lastUpdatedOn:" + lastUpdatedOn + "; ");
		}
		if (lastUpdatedBy != null) {
			buffer.append("lastUpdatedBy:" + lastUpdatedBy + "; ");
		}
		if (group != null) {
			buffer.append("groupId:" + group.getGroupId() + "; ");
		}
		return buffer.toString();
	}
}